package com.boot.core;

import com.google.auto.service.AutoService;
//import com.sun.tools.javac.api.JavacTrees;
//import com.sun.tools.javac.processing.JavacProcessingEnvironment;
//import com.sun.tools.javac.tree.JCTree;
//import com.sun.tools.javac.tree.TreeMaker;
//import com.sun.tools.javac.util.Context;
//import com.sun.tools.javac.util.Names;


import javax.annotation.processing.*;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.ExecutableType;
import javax.lang.model.util.Elements;
import javax.tools.JavaFileObject;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName : DataPrecess
 * @Description : DataPrecess
 * @Author : Xxxxx
 * @Date: 2023-02-07 09:58
 */
@SupportedAnnotationTypes("com.boot.annotation.Data")
@AutoService(Processor.class)
public class DataProcess extends AbstractProcessor {
    private String className;

    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
        System.out.println("DataProcess Processor");
        for (TypeElement annotation : annotations) {
            // 获取所有被该注解 标记过的实例
            Set<? extends Element> annotatedElements = roundEnv.getElementsAnnotatedWith(annotation);

            annotatedElements.forEach(item->{
                className= item.getSimpleName().toString();
                List<String> collect = item.getEnclosedElements().stream().filter(it -> !it.toString().contains("(")).map(it->it.toString()).collect(Collectors.toList());
                try {
                    writeBuilderFile(className+"test",collect);
                }catch (IOException e){
                    throw  new RuntimeException(e);
                }
            });
            List<? extends Element> collect = annotatedElements.stream().map(item->item.getEnclosingElement()).collect(Collectors.toList());



        }
        Set<String> supportedAnnotationTypes = getSupportedAnnotationTypes();
        System.out.println(supportedAnnotationTypes);
        return true;
    }

    private void writeBuilderFile(
            String className, List<String> fliedName)
            throws IOException {

        JavaFileObject builderFile = processingEnv.getFiler()
                .createSourceFile(className);

        try (PrintWriter out = new PrintWriter(builderFile.openWriter())) {
            out.println("package com.boot.entity;");
            out.println("public class "+className+" {");
            fliedName.forEach(item->{
                out.println("private String "+item+";");
                out.println("public String get"+getMethodName(item)+"(){");
                out.println("return this."+item+";}");
                out.println("public void set"+getMethodName(item)+"(String "+item+"){");
                out.println("this."+item+"="+item+";}");
            });
            out.println("}");
        }
    }


    private  String getMethodName(String fieldName){
        char[] chars = fieldName.toCharArray();
        chars[0] = toUpperCase(chars[0]);
        return String.valueOf(chars);
    }

    private  char toUpperCase(char c) {
        if (97 <= c && c <= 122) {
            c ^= 32;
        }
        return c;
    }


    @Override
    public synchronized void init(ProcessingEnvironment processingEnv) {
        super.init(processingEnv);
    }

    @Override
    public SourceVersion getSupportedSourceVersion() {
        return SourceVersion.latestSupported();
    }
}
